package com.google.thumbseekbardemo;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;

/**
 * When I wrote this, only God and I understood what I was doing.
 * Now, God only knows.
 * <p>
 * Created by @Harry on 2018-08-31
 */
public class Thumb extends FrameLayout {

    private int mTextColor = Color.WHITE;
    private int mHeight;
    private int mWidth;
    private double mOneProgressWidth;
    private int mCurProgress = 0;
    private String mProgressText = "";
    private int mMaxProgress = 100;
    private Paint mPaint;
    private float mThumbOffset;
    private int mTextSize = 36;


    public Thumb(Context context, AttributeSet attrs) {
        super(context, attrs);
        if (attrs != null) {
            TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.Thumb);
            mTextColor = array.getColor(R.styleable.Thumb_color, Color.WHITE);
            mTextSize = array.getDimensionPixelSize(R.styleable.Thumb_size, 36);
            array.recycle();
            float thumbWidth = array.getDimension(R.styleable.Thumb_size, 20);
            mThumbOffset = thumbWidth / 2;
        }
        initObserver();

//        View thumbView = View.inflate(context, R.layout.layout_thumb, null);
//        setThumbView(thumbView);
    }
//
//    public void setThumb(int progress, @NonNull String text) {
//        ((TextView) mThumbView.findViewById(R.id.tv)).setText(text);
//
//        float x = (float) (progress * mOneProgressWidth);
////        float textWidth = mPaint.measureText(mProgressText);
////        float textOffset = textWidth / 2;
////        if (x + textOffset > mWidth - mThumbOffset) {//超过view的右边
////            float exWidth = x + textOffset - (mWidth - mThumbOffset);
////            x -= exWidth;//避免超过右边
////        }
////        if (x + mThumbOffset < textOffset) {//超过左边
////            float exWidth = textOffset - (x + mThumbOffset);
////            x += exWidth;//避免超过左边
////        }
//
//        ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(0, 0);
//        lp.leftMargin = (int) x;
//        mThumbView.setLayoutParams(lp);
//    }
//
//    public void setThumbView(@NonNull View view) {
//        mThumbOffset = view.getMeasuredWidth() / 2;
//
//        for (int i = 0; i < this.getChildCount(); i++) {
//            View childAt = this.getChildAt(i);
//            if (mThumbView == childAt) {
//                this.removeView(mThumbView);
//            }
//        }
//
//        mThumbView = view;
//    }
//
//    public void setSrc(@DrawableRes int res) {
//        ((ImageView) mThumbView.findViewById(R.id.iv)).setImageResource(res);
//    }

    private void initObserver() {
        ViewTreeObserver vto = getViewTreeObserver();
        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

            @Override
            public boolean onPreDraw() {
                mHeight = getMeasuredHeight();
                mWidth = getMeasuredWidth();
                initPaint();
                initData();
                return true;
            }
        });
    }

    private void initPaint() {
        mPaint = new Paint();
        mPaint.setTextSize(mTextSize);
        mPaint.setTextAlign(Paint.Align.CENTER);
        mPaint.setColor(mTextColor);
    }

    private void initData() {
        mOneProgressWidth = (double) (mWidth - 2 * mThumbOffset) / (mMaxProgress);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawText(canvas);
        super.onDraw(canvas);
    }

    //设置字体居中显示
    private void drawText(Canvas canvas) {
        float x = (float) (mCurProgress * mOneProgressWidth);
        float textWidth = mPaint.measureText(mProgressText);
        float textOffset = textWidth / 2;
        if (x + textOffset > mWidth - mThumbOffset) {//超过view的右边
            float exWidth = x + textOffset - (mWidth - mThumbOffset);
            x -= exWidth;//避免超过右边
        }
        if (x + mThumbOffset < textOffset) {//超过左边
            float exWidth = textOffset - (x + mThumbOffset);
            x += exWidth;//避免超过左边
        }
//        canvas.translate(mThumbOffset, 0);
//        if (mBitmap == null) {
//            mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.shape_thumb_bg);
//        }
//
//        canvas.drawBitmap(mBitmap, new Matrix(), mPaint);
        canvas.drawText(mProgressText, x, mHeight, mPaint);
    }

    //设置显示的进度位置和字符串
    public void setProgress(int progress, String showText) {
        mCurProgress = progress;
        mProgressText = showText;
        invalidate();
    }


    private Bitmap loadBitmapFromView(View v) {

        int w = v.getWidth();

        int h = v.getHeight();

        Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

        Canvas c = new Canvas(bmp);

        c.drawColor(Color.WHITE);

        /** 如果不设置canvas画布为白色，则生成透明 */

        v.layout(0, 0, w, h);

        v.draw(c);

        return bmp;

    }
}
